<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/6/12
 * Time: 8:38 AM
 * To change this template use File | Settings | File Templates.
 */
defined('_JEXEC') or die;

class VastViewPhongtheodonvi extends JViewLegacy
{
    protected $items;
    protected $pagination;
    protected $state;
    protected $canDo;

    /**
     * Display the view
     *
     * @return    void
     */
    public function display($tpl = null)
    {
        $session = JFactory::getSession();
        
        $id = JRequest::getInt('donvi_id', 0);
        
        if ($id !== 0)
        {
            $session->set('phongtheodonvi.donvi_id', $id);
        }
        $this->items = $this->get('Items');
        $this->pagination = $this->get('Pagination');
        $this->state = $this->get('State');

        JLoader::register('DonvisHelper', JPATH_COMPONENT . '/helpers/donvi.php');

        $this->canDo = DonvisHelper::getActions();

        // Check for errors.
        if (count($errors = $this->get('Errors'))) {
            JError::raiseError(500, implode("\n", $errors));
            return false;
        }

        $this->addToolbar();

        parent::display($tpl);
    }

    /**
     * Add the page title and toolbar.
     *
     * @since    1.6
     */
    protected function addToolbar()
    {
        $session = JFactory::getSession();
        
        $id = (int)$session->get('phongtheodonvi.donvi_id');        
        
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');
        $query->where('a.id = ' . $id);

        $db->setQuery($query);
        $donvi = $db->loadObject();
        
        if ($donvi !== NULL)
        {
            JToolBarHelper::title(sprintf(JText::_('COM_VAST_MANAGER_PHONGTHEODONVI'), $donvi->ten), 'head vm_manufacturer_48');                        
        }
        else
        {
            JToolBarHelper::title(JText::_('COM_VAST_MANAGER_PHONGS'), 'head vm_manufacturer_48');            
        }
        
        if ($this->canDo->get('vast.phong.create')) {
            JToolBarHelper::addNew('phong.add', 'COM_VAST_PHONG_JTOOLBAR_NEW');
        }
        if ($this->canDo->get('vast.phong.edit')) {
            JToolBarHelper::editList('phong.edit', 'COM_VAST_PHONG_JTOOLBAR_EDIT');
        }
        if ($this->canDo->get('vast.phong.delete')) {
            JToolBarHelper::deleteList('', 'phongs.delete', 'COM_VAST_PHONG_JTOOLBAR_DELETE');
        }
        if ($this->canDo->get('core.admin')) {
            JToolBarHelper::divider();
            JToolBarHelper::preferences('com_vast', '550', '875', 'COM_VAST_PHONG_JTOOLBAR_OPTIONS');
        }
    }
}